package com.ruoyi.material.service.impl;

import java.util.List;

import com.ruoyi.material.domain.MaterialStock;
import com.ruoyi.material.mapper.MaterialStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.MaterialOperateMapper;
import com.ruoyi.material.domain.MaterialOperate;
import com.ruoyi.material.service.IMaterialOperateService;

/**
 * 操作记录模块Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-23
 */
@Service
public class MaterialOperateServiceImpl implements IMaterialOperateService 
{
    @Autowired
    private MaterialOperateMapper materialOperateMapper;

    @Autowired
    private MaterialStockMapper materialStockMapper;

    /**
     * 查询操作记录模块
     * 
     * @param operateId 操作记录模块主键
     * @return 操作记录模块
     */
    @Override
    public MaterialOperate selectMaterialOperateByOperateId(Long operateId)
    {

        return materialOperateMapper.selectMaterialOperateByOperateId(operateId);
    }

    /**
     * 查询操作记录模块列表
     * 
     * @param materialOperate 操作记录模块
     * @return 操作记录模块
     */
    @Override
    public List<MaterialOperate> selectMaterialOperateList(MaterialOperate materialOperate)
    {
        return materialOperateMapper.selectMaterialOperateList(materialOperate);
    }

    /**
     * 新增操作记录模块
     * 
     * @param materialOperate 操作记录模块
     * @return 结果
     */
    @Override
    public int insertMaterialOperate(MaterialOperate materialOperate)
    {
        //新建库存对象
        MaterialStock materialStock = new MaterialStock();
        //设置物资id和物资增加的数量
        materialStock.setMaterialId(Integer.parseInt(materialOperate.getMaterialId()));
        materialStock.setQuantity(materialOperate.getQuantity());
        materialStock.setWarehouseId(materialOperate.getWarehouseId());
        //调用mapper层，更新库存存货
        materialStockMapper.updateMaterialStockByMaterialId(materialStock);

        return materialOperateMapper.insertMaterialOperate(materialOperate);

        /**
         * update stock set quantity = #{quantity}
         * where material_id = #{materialId}
         */
    }

    /**
     * 修改操作记录模块
     * 
     * @param materialOperate 操作记录模块
     * @return 结果
     */
    @Override
    public int updateMaterialOperate(MaterialOperate materialOperate)
    {
        return materialOperateMapper.updateMaterialOperate(materialOperate);
    }

    /**
     * 批量删除操作记录模块
     * 
     * @param operateIds 需要删除的操作记录模块主键
     * @return 结果
     */
    @Override
    public int deleteMaterialOperateByOperateIds(Long[] operateIds)
    {
        return materialOperateMapper.deleteMaterialOperateByOperateIds(operateIds);
    }

    /**
     * 删除操作记录模块信息
     * 
     * @param operateId 操作记录模块主键
     * @return 结果
     */
    @Override
    public int deleteMaterialOperateByOperateId(Long operateId)
    {
        return materialOperateMapper.deleteMaterialOperateByOperateId(operateId);
    }

    /**
     * 出库操作模块
     * @param materialOperate
     * @return
     */
    @Override
    public int outputMaterial(MaterialOperate materialOperate) {

        MaterialStock materialStock = new MaterialStock();
        materialStock.setWarehouseId(materialOperate.getWarehouseId());
        materialStock.setMaterialId(Integer.parseInt(materialOperate.getMaterialId()));
        materialStock.setQuantity(materialOperate.getQuantity());
        // 更新库存记录
        materialStockMapper.updateMaterialStockOutPut(materialStock);
        // 添加一条出库的操作记录
        return materialOperateMapper.insertMaterialOperate(materialOperate);
    }
}
